﻿// <copyright file="ObjectExtensions.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace KinectExtensions
{
    #region

    using System;
    using System.Linq.Expressions;

    #endregion

    /// <summary>
    ///   The object extensions.
    /// </summary>
    public static class ObjectExtensions
    {
        #region Public Methods

        /// <summary>
        ///   The get property name.
        /// </summary>
        /// <param name = "owner">
        ///   The owner.
        /// </param>
        /// <param name = "expression">
        ///   The expression.
        /// </param>
        /// <typeparam name = "T">
        /// </typeparam>
        /// <typeparam name = "TProperty">
        /// </typeparam>
        /// <returns>
        ///   The get property name.
        /// </returns>
        /// <exception cref = "NotImplementedException">
        /// </exception>
        /// <exception cref = "NotImplementedException">
        /// </exception>
        public static string GetPropertyName<T, TProperty>(this T owner, Expression<Func<T, TProperty>> expression)
        {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression == null)
            {
                var unaryExpression = expression.Body as UnaryExpression;
                if (unaryExpression != null)
                {
                    memberExpression = unaryExpression.Operand as MemberExpression;
                    if (memberExpression == null)
                    {
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            string propertyName = memberExpression.Member.Name;
            return propertyName;
        }

        #endregion
    }
}